<?php
session_start();
require_once 'includes/class.phpmailer.php';

	require_once "includes/config.php";
	require_once "includes/pagination.php";
	
$ss = mysql_query("select googleMapKey from tbl_settings where setting_id=1") or die(mysql_error());
$tbl_settings = mysql_fetch_array($ss);
if($tbl_settings) $googleMapKey = $tbl_settings['googleMapKey'];  else $googleMapKey='';



$search_string= '';
$pro_category = '';
$pro_subcategory = '';
$srch_cond = '';
if(isset($_REQUEST['srch']))
{
	$search_string 		= $_REQUEST['search_string'];
	
	if($search_string!='')
		$srch_cond.=" AND tbl_whole_wishlist.product_name like '%".$search_string."%' ";
	
		
	
}

    $itemPerPage=10;	
	$sg = $_GET['sg'];
	$key = $_GET['key'];
	$state = $_GET['st'];
	$city = $_GET['cty'];
	$priceFrom = $_GET['pf'];
	$priceTo = $_GET['pt'];
	
	if(!empty($_GET['sg'])) {	
	
	 if(is_numeric($state))
		 $state = getStateName($state);
	 if(is_numeric($city))
		$city = getCityName($city);
		
				
	 if($city!="" || $state!="")
		  if($city!="" && $state!="") 
			$searchFor=$state." - ".$city;
		  else if($state!="")
		   	$searchFor=$state;
		  else if($city!="")
		   		$searchFor=$city;
				
	 if($_GET['sg']=="merchant")
	   {
	   if($key!='' || $state!='' || $city!='' || $priceFrom!='' || $priceTo!='')
	     {
		   $sqlSelWish="SELECT *
					   FROM tbl_whole_wishlist, tbl_users
					   WHERE tbl_users.user_type=3 and tbl_whole_wishlist.user_id = tbl_users.user_id";
					   
		   if($key!='')
		    $sqlSelWish.=" AND tbl_whole_wishlist.product_name LIKE '%$key%' "; 
		   if( $state!='')
	        $sqlSelWish.=" AND tbl_users.state LIKE '%$state%' "; 
		   if( $city!='')
	        $sqlSelWish.=" AND tbl_users.city LIKE '%$city%' "; 
		   if($priceFrom!='' && is_numeric($priceFrom))
		    $sqlSelWish.=" AND tbl_whole_wishlist.price>='$priceFrom' "; 
		   if($priceTo!='' && is_numeric($priceTo))
		    $sqlSelWish.=" AND tbl_whole_wishlist.price<='$priceTo' "; 
	     }
		 else
		    $sqlSelWish="SELECT * FROM tbl_whole_wishlist WHERE 0";		
       }
	else {
		$sqlSelWish="SELECT * FROM tbl_whole_wishlist WHERE 0";
		$pagelink="srchBuyer&sg";
	}
	$pLink = "srchBuyer&sg=$sg&key=$key&st=$state&cty=$city&pf=$priceFrom&pt=$priceTo";
	//$pLink = "srch&sg=$sg&key=$key&st=$state&cty=$city&pf=$priceFrom&pt=$priceTo";
 
	
	$pos = strripos($sqlSelWish, "where");
	if ($pos === false) {
		//echo "Not found";
	} else {
		//echo "found";
	}
	/*
if(!empty($_POST['hidBargain'])) {
$offerAmount = $_POST['txtOffer'];
$comment = $_POST['txtComment'];
if($_POST['section']=="P")
{
$link="<a href=index.php?products=".$_POST['prodId'].">".$_POST['pname']."</a>";
}
if($_POST['section']=="W")
{
$link="<a href=index.php?wishproduct=".$_POST['prodId'].">".$_POST['pname']."</a>";
}
$emailContent="You are receiving this message because (".$_SESSION['user_name'].") has made an offer towards (".$link.")<br><p>Offer: $".number_format($offerAmount,2)."<p><p>".$comment."</p>";
				
								
mysql_query("insert into tbl_messages(senderid,recieverid,productid,subject,message,posteddate,status,type,itemid,section) values('".$_SESSION['user_id_sess']."','".$_POST['recieverid']."','".$_POST['prodId']."','Offer Recieved','".addslashes($emailContent)."','".date("Y-m-d")."','1','Bargain','".$itemId."','".$_POST['section']."')");

 $mailmsg="<table width='100%' border='0' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px; line-height:25px'>
 
  <tr>
    <td height='32' colspan='2' style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;'>".$emailContent."</td>
  </tr>
  
 
</table>
";
		$mail = new PHPMailer(); // defaults to using php "mail()"
	  	$mail->MsgHTML($mailmsg);
		$mail->From       = "mail@swapen.com";
		$mail->FromName   = "Swapen.com";
		$mail->Subject    = "Offer Recieved";
		//$mail->AddAddress("suggestions@swapen.com", $firstName);
		$mail->Send();
									
									echo "<div id='suc-msg'>Offer placed successfully.</div>"; 
}
*/

?>
<script language="javascript1.2">
function sendOffer(frmid)
{
frmid.submit();
}
</script>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;oe=utf-8&amp;key=<?php echo $googleMapKey; ?>" type="text/javascript"></script>
<div id='panel'>
 <?php /*?><table>
  <tr>
    <td colspan="2" valign="top">	
      
	      
      <div style="width: 950px; float: left; margin:10px 0px 5px 0px;">
      
	 <div class="mypg_left_out">		
	     <a href="#"><img src="images/arrow_back.png" style="border:none; margin:0px 5px 0px 0px;" />Back to Search Results</a>		
	 </div>
	 
	 <div class="fullbox_out">	
	     <strong class="orangehead">Search For Buyer</strong>
	     <p style="margin: 5px 0px;" class="grey_txt">
		     <i>Take Sellings into your hands by searching through all wanted items and finding the buyers yourself.</i>
	     </p>	
	 </div>
      
      </div>
    </td>
  </tr>
 </table><?php */?>
 <table width="100%" border="0">
  <tr>
   <td>
    <div style="width: 950px; float: left; vertical-align: top;">
     <div class="mypg_left_out">
      <table style="width:100%">
       <tr>
	<td width="214" valign="top">
	<div style="float: left; margin:10px 0px 5px 0px;">
		<div class="mypg_left_out">		
		 <a href="#"><img src="images/arrow_back.png" style="border:none; margin:0px 5px 0px 0px;" />Back to Search Results</a>		
		</div>
	</div>
	 <p class="pro_ptag grey2_txt">
	  Refine Search
	 </p>
	 <div class="shourtbox">
	  <table border="0" width="100%">
	  <tbody>
	   <tr>
	    <td>
	     <form id="formShopSearch" name="formShopSearch" method="post" action="">
	     <table cellspacing="0" cellpadding="0" border="0" width="212">
	     <tbody>
	      <tr>
	       <td style="vertical-align: top;">
		<div id="category-title">Search Within Result</div>
		<br>
		<div class="fulldiv">
		 <center>
		 
		  <input type="text" style="width: 120px; float: left;" class="inputbg" id="search_string" name="search_string" value="<?php echo $search_string;?>" />
		  <input type="submit" style="height: 30px; margin: 0px 0px 0px 10px;" class="yellow_btn" value="Search" name="search" onclick="javascript:$('#srch').val('1');" />
	  
		 </center>
		</div>
	       </td>
	      </tr>
		  
	     

	      
	      
	      <tr>
	       <td style="vertical-align: top;">
		<div id="category-title">By Price</div>
		<p><a href="index.php?srchBuyer&sg=merchant&key=&st=&cty=&pf=Min&pt=Max<?php echo $pagelink; ?>&pricebtw=1">under - $10</a></p>  
		<p><a href="index.php?srchBuyer&sg=merchant&key=&st=&cty=&pf=Min&pt=Max<?php echo $pagelink; ?>&pricebtw=2">$10 - $50</a></p>  
		<p><a href="index.php?srchBuyer&sg=merchant&key=&st=&cty=&pf=Min&pt=Max<?php echo $pagelink; ?>&pricebtw=3">$50 - $100</a></p>  
		<p><a href="index.php?srchBuyer&sg=merchant&key=&st=&cty=&pf=Min&pt=Max<?php echo $pagelink; ?>&pricebtw=4">$100 - $500</a></p> 
		<p><a href="index.php?srchBuyer&sg=merchant&key=&st=&cty=&pf=Min&pt=Max<?php echo $pagelink; ?>&pricebtw=5">$500 +</a></p>           
	       </td>
	      </tr>
      
	     </tbody>
	     </table>
	     <input type="hidden" name="srch" id="srch" />
	     </form>
	    </td>
	   </tr>
	  </tbody>
	  </table>
	 </div>
	</td>
       </tr>
      </table>
     </div>
     <!--end:mypg_left_out-->
     <div class="fullbox_out">
      <table style="width: 100%;">
      <tbody>
	      <tr>
		      <td>
			      <div id='panel' style="border:none; width:700px;">
				      <strong class="orangehead">Search Results <?php if(!empty($searchFor)) echo " - ".$searchFor; ?></strong>
				      <?php
				      if(!empty($_GET['pricebtw']))
				      {
					      
					      if($_GET['pricebtw']=='1')
					      {
						      $sqlSelWish.=" AND tbl_whole_wishlist.price < 10";
					      }
					      else if($_GET['pricebtw']=='2')
					      { //$_GET['pricebtw'];
						      $sqlSelWish.=" AND tbl_whole_wishlist.price >= 10 AND tbl_whole_wishlist.price <= 50";
					      }
					      else if($_GET['pricebtw']=='3')
					      {
						      $sqlSelWish.=" AND tbl_whole_wishlist.price >= 50 AND tbl_whole_wishlist.price <= 100";
					      }
					      else if($_GET['pricebtw']=='4')
					      {
						      $sqlSelWish.=" AND tbl_whole_wishlist.price >= 100 AND tbl_whole_wishlist.price <= 500";
					      }
					      else if($_GET['pricebtw']=='5')
					      {
						      $sqlSelWish.=" AND tbl_whole_wishlist.price >500";
					      }
				      }
				      $sprice="hps";
				      if(!empty($_GET['sortby'])) {
					       if($_GET['sortby']=='lps') {
						
						$sprice="hps";
						       $sqlSelWish .= " order by tbl_whole_wishlist.price ASC";		
					       }
					       else if($_GET['sortby']=='hps') {
						 $sprice="lps";
						       $sqlSelWish .= " order by tbl_whole_wishlist.price DESC";		
					       }
					       else if($_GET['sortby']=='bm') {
						       $sqlSelWish .= " order by tbl_whole_wishlist.wishlist_id";		
					       }
					       else if($_GET['sortby']=='mst') {
						       $sqlSelWish .= " order by tbl_whole_wishlist.created_date ASC";		
					       }
				      }
				      
				      //echo $sqlSelWish;
				      $rstSelWish=mysql_query(getPagingQuery($sqlSelWish,$itemPerPage));			  
				      $num=mysql_num_rows($rstSelWish);				   
				      if($num==0)
				      {					
					      echo "No Products Found...";
				       
				      }
				      else { 
				      ?>
				      <div class="fulldiv" style="margin: 10px 0px 0px;">
					      <table align="center" width="100%" border="0">
						      <tr>
							      <td width="15%">Sort Options:</td>
							      <td width="15%"><a href="index.php?<?php echo $pLink; ?>&sortby=bm">Best Match</a></td>
							     
							      <td width="30%">
								      <div class="greytab_out" style="margin:0px 5px -9px 5px;">
									      <div class="greytab_left">&nbsp;</div>
									      <div class="greytab_repeat" style="cursor:pointer;" onclick="window.location='index.php?<?php echo $pLink;?>&sortby=<?php echo $sprice?>'">Sort Price</div>
									      <div class="greytab_right">&nbsp;</div>
								      </div>
							      </td>
						      </tr>
					      </table>
				      </div>
				      
				      
				      <form name="frmProlist" id="frmProlist" method="post" action="">
					      <input type="hidden" name="hidDelPro" id="hidDelPro">
					      <?php
						      if($_SESSION['user_id_sess']!="")
						      {
							$address_of_seller= getUserAddress($_SESSION['user_id_sess']);
						      }
					      
						      while($rowSelWish=mysql_fetch_array($rstSelWish))
						     {							   
						       $itemId=$rowSelWish['wishlist_id'];
						       $productName=wordFormater($rowSelWish['product_name'],30);
						       $productTitle = $rowSelWish['product_name'];					   
						       $productPrice=$rowSelWish['price'];
						       $wishListType=$rowSelWish['wishlist_type'];
						       $userName=$rowSelWish['username'];
						       $userCity=$rowSelWish['city'];
						       $user_id1 = $rowSelWish['user_id'];
						       $postedDate=$rowSelWish['posted_date'];
						       $wantedFor=$rowSelWish['wantedFor'];
						         if($wishListType==1) { 
							   $itemId=$rowSelWish['item_id'];
							       $pimage = getWholeProductImage($itemId,'p');
							       if($pimage['thumb']!="") {
								      $productThumb = $pimage['thumb'];
							       }  else {
								      $productThumb="images/no_image.gif";	
							       } 
							 }
							 else if(!empty($rowSelWish['product_thumb']))
							  $productThumb=$rowSelWish['product_thumb'];
							 else
							   $productThumb="images/no_image.gif"; 
							 if($wishListType=='2')
							      $link='index.php?wwishproduct='.$itemId;
							 else
							  $link='index.php?wholeproducts='.$itemId;
						       
						       
						       $address_of_user= getUserAddress($user_id1);
						       ?>
						       <input type="hidden" name="address_of_user" value="<?php echo $address_of_user;?>" id="address_of_user" />
						       <?php
						       if($_SESSION['user_id_sess']!="")
						       {
							
							?>
							<input type="hidden" name="address_of_seller" value="<?php echo $address_of_seller;?>" id="address_of_seller<?php echo $itemId;?>" />
							
							<script type="text/javascript">
							  var geocoder, location1, location2, gDir;
							  geocoder = new GClientGeocoder();
							  gDir = new GDirections();
							  
							  var address_of_seller = $('#address_of_seller<?php echo $itemId;?>').val();
							 
							  geocoder.getLocations(address_of_seller, function (response) {
							  if (!response || response.Status.code != 200)
							  {
								  //alert("Sorry, we were unable to geocode the first address");
							  }
							  else
							  {
							   
								  location1 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
								  //alert(response.Placemark[0].Point.coordinates[1]);
								  
								  
								  var address_of_user = $('#address_of_user').val();
								  geocoder.getLocations(address_of_user, function (response) {
									  if (!response || response.Status.code != 200)
									  {
										  //alert("Sorry, we were unable to geocode the second address");
									  }
									  else
									  { 
										  location2 = {lat: response.Placemark[0].Point.coordinates[1], lon: response.Placemark[0].Point.coordinates[0], address: response.Placemark[0].address};
										  gDir.load('from: ' + location1.address + ' to: ' + location2.address);
										  //alert(response.Placemark[0].Point.coordinates[1]);
									  }
								  
									  
								  });
							  }
							});
							
							GEvent.addListener(gDir, "load", function() {
							   var drivingDistanceMiles = gDir.getDistance().meters / 1609.344;
							   var drivingDistanceKilometers = gDir.getDistance().meters / 1000;
							   //document.getElementById('results').innerHTML = '<strong>Address 1: </strong>' + location1.address + ' (' + location1.lat + ':' + location1.lon + ')<br /><strong>Address 2: </strong>' + location2.address + ' (' + location2.lat + ':' + location2.lon + ')<br /><strong>Driving Distance: </strong>' + drivingDistanceMiles + ' miles (or ' + drivingDistanceKilometers + ' kilometers)';
							   var result = drivingDistanceMiles.toFixed(2);
							   $('#distance<?php echo $itemId;?>').html('<i>miles away - </i><font style="color:#000000;">'+result+' miles</font> ');
							});
						       </script>
							<?php
						       } ?>
								      <!--start-->
								      <div class="fulldiv">
									      <div class="fullbox_top"></div>
		      
									      <div class="fullbox_repeat">
										      <div class="sellpro_table">
											      <div class="sellpro_row1">
												      <img src='<?php echo $productThumb; ?>' />
											      </div>
											      
											      <div class="sellpro_row2">
												      <p class="sellpro_head pro_ptag">
												      <a href='<?php echo $link; ?>' class="sellpro_head"> <?php echo $productName; ?> </a>
												      </p><br />
												      <p class="pro_ptag">product wanted by - <?php echo $userName;?></p>
												      <p class="pro_ptag">wanted for - <font style="color:#000000;"><?php if($wantedFor==0) echo 'local pickup only'; else if($wantedFor==1) echo 'shipping only'; else echo 'local pickup or shipping'; ?></font></p>
											      </div>
											      
											      <div class="sellpro_row3" style="text-align:center;">
													  <img src="images/<?php //echo $ownerType;?>" />
												      <p style="font-size:12px;" class="pro_ptag">
													      <span id="distance<?php echo $itemId;?>"></span>
												      </p>
											      </div>
											      
											      <div class="sellpro_row2" style="text-align:right; font-size:12px;">
												      <p class="redtxt" style="margin:5px 0px 5px 0px; font-size:12px;">
													     <font style="color:#6F6F6F; font-weight:normal;">Wanted Price -</font>  $<?php echo number_format($productPrice,2);?> 
												      </p>
												      
												      <p style="margin:15px 0px 15px 0px;">
												      <a href="javascript:void(0);" style="font-size:14px; font-weight:bold;">MAKE OFFER</a>
												      </p>
												      
												      <p style="margin:15px 0px 15px 0px;">
												       <i><a href="javascript:void(0);">attach a product your selling</a></i>
												      </p>
											      </div>
										      </div>
									      </div>
									      
									      <div class="fullbox_bot"></div>
								      </div>
								      <!--end-->
					      <?php
							      }//while
						      }//else
	}
					      ?>
					      <div align="center" class="fulldiv" style="text-align:center;">
					      <?php
					       $link=$pLink;
					       echo $disp=getPagingLink($sqlSelWish,$itemPerPage,$link);							   
					      ?></div>
				      </form>
			      </div>
		      </td>
	      </tr>
	  </tbody>
	  </table>
     </div>
    
    </div>
    <!--end:950-->
   </td>
  </tr>
 </table>
</div>
<!--end:panel-->














































<?php /*
<div id='panel'>
<table width="100%" border="0">
  <tr>
    <td class="contentHeader"><h2>Search Results <?php if(!empty($searchFor)) echo " - ".$searchFor; ?></h2></td>
  </tr>
  <tr>
    <td>

		 <?php 		   
				   if(!empty($_GET['sortby'])) {
				   		if($_GET['sortby']=='lps') {
							$sqlSelWish .= " order by tbl_whole_wishlist.price ASC";		
						}
						else if($_GET['sortby']=='hps') {
							$sqlSelWish .= " order by tbl_whole_wishlist.price DESC";		
						}
						else if($_GET['sortby']=='bm') {
							$sqlSelWish .= " order by tbl_whole_wishlist.wishlist_id";		
						}
						else if($_GET['sortby']=='mst') {
							$sqlSelWish .= " order by tbl_whole_wishlist.created_date ASC";		
						}
				   }
				   echo $sqlSelWish;
				   $rstSelWish=mysql_query(getPagingQuery($sqlSelWish,$itemPerPage));			  
				   $num=mysql_num_rows($rstSelWish);				   
				   if($num==0)
					{					
					  	echo "No Products Found...";
					 
					}
				   else { ?>
					   <br><table width='670' border='0' align='center' cellspacing='0'>
						       <tr>
                                <td width="307" style="text-align:right">Sort by:</td>
						   	  <td width="359">
						  <table align="center" width="100%" border="0">
										<tr>
											<td width="22%"><a href="index.php?<?php echo $pLink; ?>&sortby=bm">Best Match</a></td>
											<td width="25%"><a href="index.php?<?php echo $pLink; ?>&sortby=lps">Most Recent</a></td>
										  <td width="27%"><a href="index.php?<?php echo $pLink; ?>&sortby=lps">Price Low-Hi</a></td>
										  <td width="26%"><a href="index.php?<?php echo $pLink; ?>&sortby=hps">Price Hi-Low</a></td>
							</tr>
								 </table></td>
                                   
					     </tr>
                       <?php
						 while($rowSelWish=mysql_fetch_array($rstSelWish))
					     {							   
						   $itemId=$rowSelWish['wishlist_id'];
						   $productName=wordFormater($rowSelWish['product_name'],30);
						   $productTitle = $rowSelWish['product_name'];					   
						   $productPrice=$rowSelWish['price'];
						   $wishListType=$rowSelWish['wishlist_type'];
						   $userName=$rowSelWish['username'];
						   $userCity=$rowSelWish['city'];
						   $postedDate=$rowSelWish['posted_date'];
						   if($wishListType==1) { 
						     $itemId=$rowSelWish['item_id'];
							 $pimage = getProductImage($itemId,'i');
							 if($pimage['thumb']!="") {
								$productThumb = $pimage['thumb'];
							 }  else {
								$productThumb="images/no_image.gif";	
							 } 
						   }
						   else if(!empty($rowSelWish['product_thumb']))
						    $productThumb=$rowSelWish['product_thumb'];
						   else
						     $productThumb="images/no_image.gif"; 
						   if($wishListType=='2')
						   {
							$link='index.php?wishproduct='.$itemId;
							$w="W";
							}
						   else
						   {
						    $link='index.php?products='.$itemId;
							$w="P";
							}
						   ?>	
                           <tr>
                                <td colspan="2"><table width="100%" border="0"  height="113" class="container">
                                    <tr>
                                      <td rowspan="3" width="85">
											<a href='<?php echo $link; ?>'>
												<img src='<?php echo $productThumb; ?>' border='0'>											</a>										</td>
                                      <td  width="10" height="43" style="font-size:14px">&nbsp;</td>
                                      <td  width="284" style="font-size:14px"><a href='<?php echo $link; ?>'><?php echo $productName; ?></a></td>
                                      <td width="110">Wishing price</td>
                                      <td colspan="2">Offer</td>
                                      </tr>
                                      <tr>
                                        <td>&nbsp;</td>
                                      <td>&nbsp;&nbsp;&nbsp;Location : <?php echo $userCity ?></td>
                                      <td><?php echo $productPrice ?></td>
                                      <td colspan="2"><form id="form1" name="frm<?php echo $itemId;?>" method="post" action="">
                                        <table width="100%" border="0">
                                          <tr>
                                            <td>Offer Price </td>
                                            <td><input type="text" name="txtOffer" /></td>
                                          </tr>
                                          <tr>
                                            <td>Comment</td>
                                            <td><textarea name="txtComment" cols="20"></textarea></td>
                                          </tr>
                                          <tr>
                                            <td>&nbsp;</td>
                                            <td><input type="submit" name="Submit" value="Make Offer" onclick="return sendOffer('<?php echo $itemId;?>')"/><input type="hidden" name="recieverid" value="<?php echo $rowSelWish['user_id'];?>"/>
                                <input type="hidden" name="pname" value="<?php echo $productName;?>"/>
								<input type="hidden" name="prodId" value="<?php echo $itemId;?>"/>
								<input type="hidden" name="section" value="<?php echo $w;?>"/>
                                <input type="hidden" name="hidBargain" value="1"/></td>
                                          </tr>
                                        </table>
                                                                            </form>
                                      </td>
                                      </tr>
                                      <tr>
                                        <td>&nbsp;</td>
                                      <td><strong><?php echo $userName;?></strong></td>
                                      <td></td>
                                      <td width="75"></td>
                                      <td width="78"></td>
                                      </tr>
                                      </table>
                             </td></tr>
							   <tr>
									 <td colspan="2">&nbsp;</td>
							   </tr>				 	
						 <?php } ?>				 
						</table>
				    <?php }
					} ?>
                                    <tr>
                                      <td><div align="center">
                                        <?php
				   $link=$pLink;
				   echo $disp=getPagingLink($sqlSelWish,$itemPerPage,$link);							   
			 ?>
                                      </div></td>
                                    </tr>
                                  </table>
                                </div>
			</td>
  		</tr>
                                </table>
	
	</td>
  </tr>
</table>
</div>
*/
?>